package Game;

import java.awt.*;
import java.io.IOException;

/**
 *
 * @author Patrice
 */
public class ClientInitFrame extends javax.swing.JFrame {

    private static final Color LIGHT_RED = new Color(255, 140, 155);
    private static final Color WHITE = Color.WHITE;
    private int port;
    private String IP;
    private Client client;

    public ClientInitFrame() {
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        initComponents();
        this.setLocation(MouseInfo.getPointerInfo().getLocation());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        textfield_port = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        button_default_port = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        textfield_nick = new javax.swing.JTextField();
        button_default_nick = new javax.swing.JButton();
        button_connect = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        textfield_IP_1 = new javax.swing.JTextField();
        textfield_IP_2 = new javax.swing.JTextField();
        label_connected = new javax.swing.JLabel();
        textfield_IP_4 = new javax.swing.JTextField();
        textfield_IP_3 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Client");
        setAlwaysOnTop(true);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel1.setText("IP:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Port:");

        button_default_port.setFont(new java.awt.Font("Tahoma", 0, 10));
        button_default_port.setText("Default");
        button_default_port.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_default_portActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Nickname:");

        button_default_nick.setFont(new java.awt.Font("Tahoma", 0, 10));
        button_default_nick.setText("Default");
        button_default_nick.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_default_nickActionPerformed(evt);
            }
        });

        button_connect.setFont(new java.awt.Font("Tahoma", 0, 10));
        button_connect.setText("Connect");
        button_connect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_connectActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel4.setText("ClientSettings");

        textfield_IP_1.setBackground(new java.awt.Color(240, 240, 240));
        textfield_IP_1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        textfield_IP_1.setText("192");
        textfield_IP_1.setBorder(textfield_IP_4.getBorder());
        textfield_IP_1.setFocusable(false);

        textfield_IP_2.setBackground(new java.awt.Color(240, 240, 240));
        textfield_IP_2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        textfield_IP_2.setText("168");
        textfield_IP_2.setBorder(textfield_IP_4.getBorder());
        textfield_IP_2.setFocusable(false);

        label_connected.setFont(new java.awt.Font("Arial", 1, 14));
        label_connected.setForeground(new java.awt.Color(204, 0, 0));
        label_connected.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        label_connected.setText("No");
        label_connected.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "connected", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Tahoma", 0, 9))); // NOI18N

        textfield_IP_4.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        textfield_IP_3.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel5.setText(".");

        jLabel6.setText(".");

        jLabel7.setText(".");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                        .addComponent(label_connected, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(textfield_IP_1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(2, 2, 2)
                        .addComponent(jLabel5)
                        .addGap(2, 2, 2)
                        .addComponent(textfield_IP_2, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(2, 2, 2)
                        .addComponent(jLabel6)
                        .addGap(2, 2, 2)
                        .addComponent(textfield_IP_3, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(2, 2, 2)
                        .addComponent(jLabel7)
                        .addGap(2, 2, 2)
                        .addComponent(textfield_IP_4, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(textfield_port, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                    .addComponent(textfield_nick, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE)
                        .addComponent(button_default_port, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE)
                        .addComponent(button_default_nick, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(button_connect, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_connected, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textfield_IP_1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(textfield_IP_2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(textfield_IP_3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(textfield_IP_4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(button_default_port, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textfield_port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(button_default_nick, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textfield_nick, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(button_connect, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(47, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(201, 201, 201))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void button_default_portActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_default_portActionPerformed
        textfield_port.setText("1234");
    }//GEN-LAST:event_button_default_portActionPerformed

    private void button_default_nickActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_default_nickActionPerformed
        textfield_nick.setText(System.getProperty("user.name"));
    }//GEN-LAST:event_button_default_nickActionPerformed

    private void button_connectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_connectActionPerformed
        if (check()){
            try {
                client = new Client(IP, port);
                this.setDefaultCloseOperation(HIDE_ON_CLOSE);
                label_connected.setForeground(new Color(0, 102, 0));
                label_connected.setText("Yes");
                try {
                    client.close();
                }
                catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            catch (Exception ex) {}
        }
    }//GEN-LAST:event_button_connectActionPerformed

    private boolean check(){
        int verify = 0;
        if (!textfield_IP_3.getText().isEmpty() && !textfield_IP_4.getText().isEmpty()){
            verify++;
            textfield_IP_3.setBackground(WHITE);
            textfield_IP_4.setBackground(WHITE);
            IP = textfield_IP_1.getText() + "." + textfield_IP_2.getText() + "." + textfield_IP_3.getText() + "." + textfield_IP_4.getText();
        }
        else {
            textfield_IP_3.setBackground(LIGHT_RED);
            textfield_IP_4.setBackground(LIGHT_RED);
        }
        if (!textfield_nick.getText().isEmpty()){
            verify++;
            textfield_nick.setBackground(WHITE);
        }
        else{
            textfield_nick.setBackground(LIGHT_RED);
        }

        boolean isNumber = true;
        try {
            Integer.parseInt(textfield_port.getText());
            port = Integer.parseInt(textfield_port.getText());
        }
        catch (Exception ex){
            isNumber = false;
        }

        if (!textfield_port.getText().isEmpty() && isNumber){
            verify++;
            textfield_port.setBackground(WHITE);
        }
        else {
            textfield_port.setBackground(LIGHT_RED);
        }
        return (verify==3) ? true : false;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClientInitFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button_connect;
    private javax.swing.JButton button_default_nick;
    private javax.swing.JButton button_default_port;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel label_connected;
    private javax.swing.JTextField textfield_IP_1;
    private javax.swing.JTextField textfield_IP_2;
    private javax.swing.JTextField textfield_IP_3;
    private javax.swing.JTextField textfield_IP_4;
    private javax.swing.JTextField textfield_nick;
    private javax.swing.JTextField textfield_port;
    // End of variables declaration//GEN-END:variables

}
